package person.bing.fbcloud.controller;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import person.bing.fbcloud.api.dto.PostCommentDTO;
import person.bing.fbcloud.api.entity.PostComment;
import person.bing.fbcloud.common.core.exception.CheckedException;
import person.bing.fbcloud.common.core.util.R;
import person.bing.fbcloud.repository.PostCommentRepository;
import person.bing.fbcloud.service.PostCommentService;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author mengfb
 * @since 2019-06-24
 */
@RestController
@RequestMapping("/postcomment")
public class PostCommentController {
    @Autowired
    private PostCommentService postCommentService;
    @Autowired
    private PostCommentRepository postCommentRepository;

    /**
     * 添加评论
     * @param postCommentDTO
     * @return
     */
    @PostMapping
    public R addPostComment(@RequestBody PostCommentDTO postCommentDTO){
        String comment = postCommentDTO.getComment();
        String postId = postCommentDTO.getPostId();
        String userCode = postCommentDTO.getUserCode();
        if (StrUtil.isEmpty(comment)){
            throw new CheckedException("comment不能为空");
        }
        if (StrUtil.isEmpty(postId)){
            throw new CheckedException("postId不能为空");
        }
        if (StrUtil.isEmpty(userCode)){
            throw new CheckedException("userCode不能为空");
        }

        postCommentService.addPostComment(comment,userCode,postId);
        PostComment postComment = new PostComment();
        postComment.setComment(comment);
        postComment.setUserCode(userCode);
        postComment.setPostId(postId);
        postCommentRepository.save(postComment);
        return new R();
    }

    /**
     * 根据postId获取评论
     * @param postId
     * @return
     */
    @GetMapping("/{postId}")
    public R getPostCommentByPostId(@PathVariable String postId){
        return new R(postCommentService.getPostComment(postId));
    }

    /**
     * es测试
     * @param comment
     * @return
     */
    @GetMapping("/es")
    public R esTest(String comment){
        return new R(postCommentRepository.findByComment(comment));
    }

}
